package com.ffcs.cn.action.desktopcontrol.smtextfieldlegit;

import com.supermap.desktop.controls.ui.controls.SmDialog;
import com.supermap.desktop.controls.ui.controls.textFields.NumberFieldLegit;
import com.supermap.desktop.controls.ui.controls.textFields.SmTextFieldLegit;
import com.supermap.desktop.core.ui.controls.GridBagConstraintsHelper;

import java.awt.*;

/**
 * 弹出具有合法性验证的文本输入框的示例对话框
 *
  */
public class DialogSmTextFieldLegit extends SmDialog {
	private SmTextFieldLegit smTextFieldLegit;

	public DialogSmTextFieldLegit() {
		setSize(new Dimension(300, 100));
		setTitle("具有合法性验证的文本输入框");
		initComponents();
		initLayout();
	}

	public void initComponents() {
		smTextFieldLegit = new NumberFieldLegit();
		// 设置是否合法
		smTextFieldLegit.setSmTextFieldLegit(new NumberFieldLegit() {
			// 判断是否合法
			@Override
			public boolean isTextFieldValueLegit(String textFieldValue) {
				// 只有在输入的长度不大于5的时候才合法
				return textFieldValue.length() <= 5;
			}

			// 在输入不合法的情况下，获取一个合法值，这里设置为上一次输入的合法值
			@Override
			public String getLegitValue(String currentValue, String backUpValue) {
				return backUpValue;
			}
		});
		// 输入非法时的提示信息
		smTextFieldLegit.setWarningText("输入长度不应该大于5");
	}

	public void initLayout() {
		this.setLayout(new GridBagLayout());
		this.add(smTextFieldLegit, new GridBagConstraintsHelper(0, 0, 1, 1)
				.setInsets(GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.FRAME_CONTROL_GAP, GridBagConstraintsHelper.CONTROLS_GAP)
				.setWeight(1, 0)
				.setFill(GridBagConstraints.BOTH));
	}
}
